<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="编号" prop="name">
        <el-input v-model="ruleForm.uId" :disabled="true">{{ruleForm.uId}}</el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name" >
        <el-input v-model="ruleForm.name">{{ruleForm.name}}</el-input>
      </el-form-item>
      <el-form-item label="权限" prop="name">
        <el-input v-model="ruleForm.uType" :disabled="true">{{ruleForm.uType}}</el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="editUser('ruleForm')">{{buttonText}}</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserInfo } from "../../utils/dataStorage";

import { setUserInfo } from "../../utils/dataStorage";
import { getMap } from "../../utils/dataStorage";
import { getUser } from "./Index";
import { editUser } from "../../api/user";
export default {
  inject: ["reload"],
  props: ["uId"],
  data() {
    name: "editName";
    return {
      ruleForm: {
        uId: "",
        name: "",
        password: "",
        uType: "",
        desc: ""
      },
      
      rules: {
         name: [
          { required: true, message: "请输入客户姓名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1-10个字符", trigger: "blur" }
        ],
      },

      buttonText: "保存"
    };
  },
  created() {
    this.ruleForm.name = getUserInfo().username;
    this.ruleForm.uId = getUserInfo().uId;
    this.ruleForm.uType = getUserInfo().uType;
    
  },
  components: {},
  methods: {
    
    resetForm(formName){
        this.$refs[formName].resetFields();
    },
    editUser() {
      // 设置提交的参数
      let param = new URLSearchParams();
      param.append("uId", this.ruleForm.uId);
      param.append("username", this.ruleForm.name);

      param.append("uType", this.ruleForm.uType);

      editUser(param)
        .then(r => {
          setTimeout(() => {
            setUserInfo(r);
             console.log(r)
            this.$notify({
              title: "修改名称成功啦啦啦",
              type: "success"
            });
          }, 1000);
          //本来还能用
         // this.$layer.closeAll(); //疯狂模式，关闭所有层
         // location.reload(true) //关闭刷新
        })
        .catch(_ => {
          this.$notify({
              title: "修改名称失败",
              type: "false"
            });
          console.log("修改失败没名字");
        });
    }
    // else {
    //     console.log("error submit!!");
    //     return false;
    // }
  }
};
</script>

<style scoped>
</style>
